package test.wrc.com.app;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class ActivityActivity extends BaseActivity {

    Button activity1, activity2, activity3, activity4;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_activity);

        activity1 = (Button) findViewById(R.id.activity1);
        activity1.setOnClickListener(this);
        activity2 = (Button) findViewById(R.id.activity2);
        activity2.setOnClickListener(this);
        activity3 = (Button) findViewById(R.id.activity3);
        activity3.setOnClickListener(this);
        activity4 = (Button) findViewById(R.id.activity4);
        activity4.setOnClickListener(this);

        if (savedInstanceState != null) {
            String data = savedInstanceState.getString("data");
            Log.i(TAG, "onCreate:" + data);
            Toast.makeText(this, "回传值:onCreate:" + data, Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onClick(View v) {
        Intent intent = null;
        switch (v.getId()) {
            case R.id.activity1:
                Toast.makeText(this, "Intent:startActivity", Toast.LENGTH_SHORT).show();
                intent = new Intent();
                intent.setClass(this, LoginActivity.class);
                startActivity(intent);
                break;
            case R.id.activity2:
                Toast.makeText(this, "Intent:startActivityForResult", Toast.LENGTH_SHORT).show();
                intent = new Intent();
                intent.setClass(this, LoginActivity.class);
                intent.putExtra("username", "test");
                startActivityForResult(intent, 1000);//回传值
                break;
            case R.id.activity3:
                Toast.makeText(this, "Intent:浏览器", Toast.LENGTH_SHORT).show();
                intent = new Intent(Intent.ACTION_VIEW);
                intent.setData(Uri.parse("http://www.baidu.com"));
                startActivity(intent);
                break;
            case R.id.activity4:
                Toast.makeText(this, "Intent:打电话", Toast.LENGTH_SHORT).show();
                intent = new Intent(Intent.ACTION_DIAL);
                intent.setData(Uri.parse("tel:10086"));
                startActivity(intent);
                break;
        }
    }

    /**
     * Activity 回传值
     *
     * @param requestCode
     * @param resultCode
     * @param data
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 1000) {
            if (resultCode == RESULT_OK) {
                String username = data.getStringExtra("username");
                Toast.makeText(this, "登录成功:" + username, Toast.LENGTH_SHORT).show();
            } else if (resultCode == RESULT_CANCELED) {
                Toast.makeText(this, "取消登录", Toast.LENGTH_SHORT).show();
            }
        }
    }

    //============Activity回传值================
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        //通过onSaveInstanceState存值
        outState.putString("data", "王儒超");
        Log.i(TAG, "onSaveInstanceState");
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        if (savedInstanceState != null) {
            String data = savedInstanceState.getString("data");
            Log.i(TAG, "onRestoreInstanceState:" + data);
            Toast.makeText(this, "回传值:onRestoreInstanceState:" + data, Toast.LENGTH_SHORT).show();
        }
        Log.i(TAG, "onRestoreInstanceState");
    }
    //============Activity回传值================
}
